package utils

import (
	"database/sql"
	"fmt"
	pb "gitee.com/hakwolf/taibak/internal/modules/rpc/proto"
	"strconv"
	"strings"
)

type Mssql struct {
	*sql.DB
	DataSource string
	Database   string
	Windows    bool
	Sa         SA
}

type SA struct {
	User   string
	Passwd string
}

func (m *Mssql) Open() (err error) {
	var conf []string
	conf = append(conf, "Provider=SQLOLEDB")
	conf = append(conf, "Data Source="+m.DataSource)
	if m.Windows {
		// Integrated Security=SSPI 这个表示以当前WINDOWS系统用户身去登录SQL SERVER服务器(需要在安装sqlserver时候设置)，
		// 如果SQL SERVER服务器不支持这种方式登录时，就会出错。
		//	conf = append(conf, "integrated security=SSPI")
	}
	conf = append(conf, "Initial Catalog="+m.Database)
	conf = append(conf, "user id="+m.Sa.User)
	conf = append(conf, "password="+m.Sa.Passwd)

	m.DB, err = sql.Open("adodb", strings.Join(conf, ";"))
	if err != nil {
		return err
	}
	return nil
}

func (m *Mssql) GetDsn() (ret string) {
	var conf []string
	conf = append(conf, "Provider=SQLOLEDB")
	conf = append(conf, "Data Source="+m.DataSource)
	if m.Windows {
		// Integrated Security=SSPI 这个表示以当前WINDOWS系统用户身去登录SQL SERVER服务器(需要在安装sqlserver时候设置)，
		// 如果SQL SERVER服务器不支持这种方式登录时，就会出错。
		//	conf = append(conf, "integrated security=SSPI")
	}
	conf = append(conf, "Initial Catalog="+m.Database)
	conf = append(conf, "user id="+m.Sa.User)
	conf = append(conf, "password="+m.Sa.Passwd)

	return strings.Join(conf, ";")

}

func main() {
	db := Mssql{
		DataSource: "127.0.0.1,8888",
		Database:   "master",
		// windwos: true 为windows身份验证，false 必须设置sa账号和密码
		Windows: true,
		Sa: SA{
			User:   "sa",
			Passwd: "admin",
		},
	}
	// 连接数据库
	err := db.Open()
	if err != nil {
		fmt.Println("sql open:", err)
		return
	}
	defer db.Close()

	// 执行SQL语句
	rows, err := db.Query("select * from info")
	if err != nil {
		fmt.Println("query: ", err)
		return
	}
	for rows.Next() {
		var name string
		var number int
		rows.Scan(&name, &number)
		fmt.Printf("Name: %s \t Number: %d\n", name, number)
	}
}

func EXECadodbsql(req *DbTaskRequest, sql string) (err error) {
	var dataSource string
	if req.Port != 0 {
		strInt64 := strconv.FormatInt(req.Port, 10)
		id16, _ := strconv.Atoi(strInt64)
		dataSource = req.Dip + "," + strconv.Itoa(id16) //setting.Db.Host
	} else {
		dataSource = req.Dip
	}
	db := Mssql{
		DataSource: dataSource,
		Database:   req.Dbname,
		// windwos: true 为windows身份验证，false 必须设置sa账号和密码
		Windows: false,
		Sa: SA{
			User:   req.Ac,
			Passwd: req.Pd,
		},
	}
	// 连接数据库
	ers := db.Open()
	if ers != nil {
		fmt.Println("sql open:", err)
		return ers
	}
	defer db.Close()

	// 执行SQL语句
	_, ers = db.Exec(sql)
	if err != nil {
		fmt.Println("query: ", err)
		return ers
	}
	return ers
}

func EXECadodbsql2(req *pb.DbTaskRequest, sql string) (err error) {
	var dataSource string
	if req.Port != 0 {
		strInt64 := strconv.FormatInt(req.Port, 10)
		id16, _ := strconv.Atoi(strInt64)
		dataSource = req.Dip + "," + strconv.Itoa(id16) //setting.Db.Host
	} else {
		dataSource = req.Dip
	}
	db := Mssql{
		DataSource: dataSource,
		Database:   req.Dbname,
		// windwos: true 为windows身份验证，false 必须设置sa账号和密码
		Windows: false,
		Sa: SA{
			User:   req.Ac,
			Passwd: req.Pd,
		},
	}
	// 连接数据库
	ers := db.Open()
	if ers != nil {
		fmt.Println("sql open:", err)
		return ers
	}
	defer db.Close()

	// 执行SQL语句
	_, ers = db.Exec(sql)
	if err != nil {
		fmt.Println("query: ", err)
		return ers
	}
	return ers
}
